import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

@Injectable()
export class RolesGuard implements CanActivate {
    constructor(private reflector: Reflector) { }

    canActivate(context: ExecutionContext): boolean {
        const roles = this.reflector.get<string[]>('roles', context.getHandler());
        if (!roles) {
            return true;
        }
        const request = context.switchToHttp().getRequest();
        const user = request.user;

        return user && user.roles && this.matchRoles(roles, user.roles);
    }

    private matchRoles(allowedRoles: string[], userRoles: string[]): boolean {
        return allowedRoles.some(role => userRoles.includes(role));
    }
} 